package com.recipedb.recipedb.repository;

import com.recipedb.recipedb.entity.UserBrowse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserBrowseRepository extends JpaRepository<UserBrowse, Long> {

    List<UserBrowse> findByUserIdOrderByCreatedAtDesc(Long userId);

    @Modifying
    @Query("DELETE FROM UserBrowse ub WHERE ub.id = :id AND ub.user.id = :userId")
    int deleteByIdAndUserId(@Param("id") Long id, @Param("userId") Long userId);

    List<UserBrowse> findAllByOrderByCreatedAtDesc();

    // 根据用户ID和菜谱ID查找浏览记录
    Optional<UserBrowse> findByUserIdAndRecipeId(Long userId, Long recipeId);
}